package com.xr.controller;

import com.alibaba.druid.sql.visitor.functions.Concat;
import com.alibaba.dubbo.config.annotation.Reference;
import com.xr.constant.MessageConstant;
import com.xr.exception.CustomException;
import com.xr.pojo.CheckItem;
import com.xr.query.QueryPageBean;
import com.xr.result.PageResult;
import com.xr.result.Result;
import com.xr.service.CheckItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
 * 检查列表Controller
 * */

@RestController
@RequestMapping("checkItem")
public class CheckItemController {

    @Reference //远程订阅服务注解注入
    private CheckItemService checkItemService;

//    分页+条件查询
    @RequestMapping("/findPage")
//    @PreAuthorize("hasAnyAuthority('aaa')")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
//        System.out.println("join findPage====>"+queryPageBean);
        PageResult page = checkItemService.findPage(queryPageBean);
        return  new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,page);
    }
//    添加检查列表信息
    @RequestMapping("/insertCheckItem")
    public Result insertCheckItem(@RequestBody CheckItem checkItem){
        System.out.println("join insertCheckItem===========>"+checkItem);
        try {
            checkItemService.insertCheckItem(checkItem);
            return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
    }
//    删除检查
    @PostMapping("/deleteCheckItem")
    @PreAuthorize("hasAnyAuthority('ddd')")
    public Result deleteCheckItem(Integer id) throws CustomException {
        System.out.println("join deleteCheckItem=====>"+id);
        checkItemService.deleteCheckItem(id);
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }
//    修改检查项
    @RequestMapping("/updateCheckItem")
    public Result updateCheckItem(@RequestBody CheckItem checkItem){
        System.out.println("join updateCheckItem========>"+checkItem);
        try {
            checkItemService.updateCheckItem(checkItem);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
    }
//    根據ID查詢檢查項
    @RequestMapping("/queryById")
    public Result queryById(Integer id){
        System.out.println("join queryById====>"+id);
        CheckItem checkItem = checkItemService.queryById(id);
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkItem);
    }
//  查詢所有檢查項
    @RequestMapping("/queryAllCheckItem")
    public Result  queryAll(){
        System.out.println("join queryALl======>");
        List<CheckItem> all = checkItemService.queryAll();
        if(all==null){
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,all);
    }
}